#include "BinaryWriter.h"
#include "StringUtil.h"

#include <io.h>
#include <sys/types.h>  // For stat().
#include <sys/stat.h>   // For stat().

BinaryWriter::BinaryWriter()
{
}

BinaryWriter::BinaryWriter(tstring filename):m_FileName(filename)
{
}

BinaryWriter::~BinaryWriter(void)
{
}

bool BinaryWriter::Open()
{
	tstring parentDir = StringUtil::GetParentDirectory(m_FileName);
	StringUtil::MakeDirectories(parentDir);
	
	m_Output.open (m_FileName.c_str(), ios::out | ios::binary);
	m_Open = m_Output.is_open();
	return m_Open;
}

void BinaryWriter::WriteString(tstring& string)
{
	int stringType = sizeof(TCHAR);
	m_Output.write((char*)&stringType,sizeof(int));

	int size = string.size() * sizeof(TCHAR);
	m_Output.write((char*)&size,sizeof(int));

	const TCHAR* rawString = string.c_str();
	m_Output.write((char*)rawString, size);
}

void BinaryWriter::Close(){
	if ( m_Output){
		m_Output.close();
	}
}
